import { http } from '@/request/http'
import type { PageDTO, PageVO } from '@/type/type'
import type { LabelVO } from '@/request/api/label'
import { initLabelVO } from '@/request/api/label'
import type { MakeOptional } from '@/type'

type BaseArticle = {
  // 自增主键
  id: number
  // 标题
  title: string
  // 封面
  cover: string
  // 内容
  content: string
  // 点赞量
  likeCount: number
  // 浏览量
  viewCount: number
  // 是否含有视频
  hasVideo: boolean
  // 视频链接
  videoUrl: string
  // 密码提示
  tips: string
  // 是否推荐
  isRecommend: boolean
  // 是否可评论
  isComment: boolean
  // 修改人ID
  origin: string
  // 是否启用
  status: boolean
}

export type ArticleDTO = MakeOptional<BaseArticle> & {
  // 标签ID
  labelId: number | undefined
  // 文章密码
  password: string | undefined
}

export type ArticleVO = BaseArticle & {
  // 评论数量
  commentCount: number
  // 创建时间
  createTime: Date
  // 更新时间
  updateTime: Date
  // 标签
  label: LabelVO
}

export type ArchiveVO = {
  // 类别ID
  classifyId: number
  // 类别名
  classifyName: string
  // 标签列表
  labelList: ArticleLabelVO[]
}

type ArticleLabelVO = {
  // 标签ID
  labelId: number
  // 标签名
  labelName: string
  // 文章数量
  articleCount: string
  // 封面
  imgUrl: string
}

export const initArticleDTO = (): ArticleDTO => {
  return {
    id: undefined,
    labelId: undefined,
    title: undefined,
    cover: undefined,
    content: undefined,
    likeCount: undefined,
    viewCount: undefined,
    hasVideo: undefined,
    videoUrl: undefined,
    tips: undefined,
    isRecommend: undefined,
    isComment: undefined,
    origin: undefined,
    status: undefined,
    password: undefined
  }
}

export const initArticleVO = (): ArticleVO => {
  return {
    id: 0,
    title: '',
    label: initLabelVO(),
    cover: '',
    content: '',
    likeCount: 0,
    viewCount: 0,
    hasVideo: false,
    videoUrl: '',
    tips: '',
    isRecommend: false,
    isComment: false,
    origin: '',
    commentCount: 0,
    createTime: new Date(),
    updateTime: new Date(),
    status: true
  }
}

export const articleVO2DTO = (vo: ArticleVO): ArticleDTO => {
  return {
    id: vo.id,
    labelId: vo.label.id,
    title: vo.title,
    cover: vo.cover,
    content: vo.content,
    likeCount: vo.likeCount,
    viewCount: vo.viewCount,
    hasVideo: vo.hasVideo,
    videoUrl: vo.videoUrl,
    tips: vo.tips,
    isRecommend: vo.isRecommend,
    isComment: vo.isComment,
    origin: vo.origin,
    status: vo.status,
    password: undefined
  }
}

export const uploadArticleImgApi = (data: FormData) => {
  return http<string[]>({
    url: `/article/upload/files`,
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data: data
  })
}

export const getArticleApi = (id: number) => {
  return http<ArticleVO>({
    url: `/article/${id}`,
    method: 'get'
  })
}

export const getArticleRecommendApi = () => {
  return http<ArticleVO[]>({
    url: `/article/recommend/list`,
    method: 'get'
  })
}

export const getArticleDeployApi = () => {
  return http<ArticleVO[]>({
    url: `/article/deploy/list`,
    method: 'get'
  })
}

export const getArticlePageApi = (page: PageDTO) => {
  return http<PageVO<ArticleVO>>({
    url: `/article/page`,
    method: 'get',
    params: {
      dto: JSON.stringify(page)
    }
  })
}

// 获取归档列表
export const getArchiveListApi = () => {
  return http<ArchiveVO[]>({
    url: `/article/archive/list`,
    method: 'get'
  })
}

export const getArticleAdminPageApi = (page: PageDTO) => {
  return http<PageVO<ArticleVO>>({
    url: `/admin/article/page`,
    method: 'get',
    params: {
      dto: JSON.stringify(page)
    }
  })
}

export const addArticleApi = (dto: ArticleDTO, urls: string[]) => {
  return http<boolean>({
    url: '/admin/article/insert',
    method: 'POST',
    data: {
      article: dto,
      urls: urls
    }
  })
}

// 修改
export const updateArticleApi = (dto: ArticleDTO, urls: string[]) => {
  return http<boolean>({
    url: '/admin/article/update',
    method: 'PUT',
    data: {
      article: dto,
      urls: urls
    }
  })
}
